package com.hgx.captcha.server.config;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import java.util.Properties;

@Configuration
public class KaptchaConfig {

    @Autowired
    private Environment env;

    @Bean
    public DefaultKaptcha producer() {
        Properties properties = new Properties();

        //边框
        properties.put("kaptcha.border", "no");
        properties.put("kaptcha.border.color", env.getProperty("kaptcha.border.color"));
        properties.put("kaptcha.border.thickness", env.getProperty("kaptcha.border.thickness"));
        //图片
        properties.put("kaptcha.image.width", env.getProperty("kaptcha.image.width"));
        properties.put("kaptcha.image.height", env.getProperty("kaptcha.image.height"));
        //图片实现类
        properties.put("kaptcha.producer.impl", env.getProperty("kaptcha.producer.impl"));

        //文本
        properties.put("kaptcha.textproducer.char.space", env.getProperty("kaptcha.textproducer.char.space"));
        properties.put("kaptcha.textproducer.char.length",env.getProperty("kaptcha.textproducer.char.length"));
        properties.put("kaptcha.textproducer.char.string",env.getProperty("kaptcha.textproducer.char.string"));
        properties.put("kaptcha.textproducer.font.name", env.getProperty("kaptcha.textproducer.font.name"));
        properties.put("kaptcha.textproducer.font.size", env.getProperty("kaptcha.textproducer.font.size"));
        properties.put("kaptcha.textproducer.font.color", env.getProperty("kaptcha.textproducer.font.color"));

        //干扰
        properties.put("kaptcha.noise.impl", env.getProperty("kaptcha.noise.impl"));
        properties.put("kaptcha.noise.color", env.getProperty("kaptcha.noise.color"));

        //图片样式
        properties.put("kaptcha.background.impl", env.getProperty("kaptcha.background.impl"));
        //渐变
        properties.put("kaptcha.background.clear.from", env.getProperty("kaptcha.background.clear.from"));
        properties.put("kaptcha.background.clear.to", env.getProperty("kaptcha.background.clear.to"));

        //文字渲染器
        properties.put("kaptcha.word",env.getProperty("kaptcha.word"));

        //session
        properties.put("kaptcha.session.key",env.getProperty("kaptcha.session.key"));
        properties.put("kaptcha.session.date",env.getProperty("kaptcha.session.date"));

        Config config = new Config(properties);
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        defaultKaptcha.setConfig(config);

        return defaultKaptcha;
    }
}